from sqlalchemy import Column, Integer, Date, String, ForeignKey, Enum as SqlEnum
from sqlalchemy.orm import relationship

from database import Base


class DocSchedule(Base):
    __tablename__ = "schedule_doc"

    ID = Column(Integer, primary_key=True, index=True, autoincrement=True)
    doctor_id = Column(Integer, nullable=False)  # 医生ID
    schedule_date = Column(Date, nullable=False)  # 排班日期
    department = Column(String(50), nullable=False)  # 科室
    am_remaining = Column(Integer, default=0)  # AM余号数量
    pm_remaining = Column(Integer, default=0)  # PM余号数量

    def to_dict(self):
        return {
            "ID": self.ID,
            "doctor_id": self.doctor_id,
            "schedule_date": self.schedule_date,
            "department": self.department,
            "am_remaining": self.am_remaining,
            "pm_remaining": self.pm_remaining
        }


class Booking(Base):
    __tablename__ = "booking"

    ID = Column(Integer, primary_key=True)
    patient_ID = Column(Integer, ForeignKey("patient.ID"))  # 必须明确指定外键
    doctor_ID = Column(Integer, ForeignKey("doctor.ID"))
    book_date = Column(Date)
    book_time = Column(String)
    department = Column(String(50))
    whether_done = Column(SqlEnum("Y", "N", name="done_status"), default="N")   #新增医生修改是否处理功能

    # 定义关系时建议使用 back_populates（双向关系）
    patient = relationship("Patient", back_populates="bookings")

    # 添加关系，用于加载关联的 Doctor 对象
    # 这表示 Booking 和 Doctor 之间是多对一的关系 (一个医生可以有多条预约记录)
    doctor = relationship("Doctor", primaryjoin="Booking.doctor_ID == Doctor.ID")

    def to_dict(self):
        return {
            "ID": self.ID,
            "patient_ID": self.patient_ID,
            "book_date": self.book_date.isoformat(),
            "book_time": self.book_time,
            "doctor_ID": self.doctor_ID,
            "department": self.department,
            "whether_done":self.whether_done
        }
